import { Component } from '@angular/core';
import { NavController, NavParams, ViewController, AlertController } from 'ionic-angular';
import { DataService } from '../../services/data';

/*
  Generated class for the Register page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
  providers: [DataService]  
})
export class RegisterPage {

  name:string;
  email:string;
  password:string;
  confirmPassword:string;
  canBack:boolean = true;

  constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController, public viewCtrl: ViewController, public dataService: DataService) {}

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterPage');
  }

  register(){

    if(this.password != this.confirmPassword){
      this.showMessage('密码不一致，请重新输入密码');
      return;
    }

    this.dataService.register(this.name, this.email, this.password).subscribe(data => {
      console.log(data);
      if(data.code === 0){
        this.viewCtrl.dismiss();
        this.showMessage('注册成功!');
      }else{
        this.showMessage(JSON.stringify(data.msg));
      }
    });
  }

  showMessage(msg){
    let alert = this.alertCtrl.create({
        title: '提示',
        subTitle: msg,
        buttons: [
          {
            text: 'OK',
            handler: () => {
            }
          }
        ]
      });
      alert.present();
  }

  dismiss() {
    this.viewCtrl.dismiss();
  }
}